﻿using UnityEngine;
using GameFramework;

namespace MYSTARFORCE
{
    /// <summary>
    /// 可作为目标的实体的逻辑
    /// </summary>
    public abstract class TargetableObject : Entity
    {
        [SerializeField]
        private TargetableObjectData m_TargetableObjectData = null;

        /// <summary>
        /// 获取碰撞数据
        /// </summary>
        /// <returns>ImpactData</returns>
        public abstract ImpactData GetImpactData();

        /// <summary>
        /// 受到伤害
        /// </summary>
        /// <param name="attacker"></param>
        /// <param name="damageHP"></param>
        public void ApplyDamage(Entity attacker, int damageHP)
        {
            float fromHPRatio = m_TargetableObjectData.HPRatio;
            m_TargetableObjectData.HP -= damageHP;
            float toHPRatio = m_TargetableObjectData.HPRatio;
            if(fromHPRatio > toHPRatio)
            {
                // TODO: 显示血条动画
            }

            // 如果物体的血量小于等于0，就死亡
            if (m_TargetableObjectData.HP <= 0)
            {
                OnDead(attacker);
            }
        }

        /// <summary>
        /// 实体是否已死亡
        /// </summary>
        public bool IsDead
        {
            get
            {
                // 当HP小于等于0，就将实体标记为“已死亡”
                return m_TargetableObjectData.HP <= 0;
            }
        }

        protected override void OnInit(object userData)
        {
            base.OnInit(userData);

            //// TODO：要开启下面的语句，需要在unity中先设置一下layer
            //gameObject.SetLayerRecursively(Constant.Layer.TargetableObjectLayerId);
        }

        protected override void OnShow(object userData)
        {
            base.OnShow(userData);

            m_TargetableObjectData = (TargetableObjectData)userData;
            if(m_TargetableObjectData == null)
            {
                Log.Error("Targetable objcet data is invalid");
                return;
            }
        }

        /// <summary>
        /// 当实体死亡的时候执行的方法
        /// </summary>
        /// <param name="attacker">攻击者</param>
        protected virtual void OnDead(Entity attacker)
        {
            // 如果死亡，就隐藏掉
            GameEntry.Entity.HideEntity(this);
        }

        /// <summary>
        /// 当触发碰撞组件的OnTriggerEnter方法时
        /// </summary>
        /// <param name="other">另外的碰撞体</param>
        private void OnTriggerEnter(Collider other)
        {
            // 获取另外的碰撞体身上的entity组件
            Entity entity = other.gameObject.GetComponent<Entity>();
            if(entity == null)
            {
                return;
            }

            if(entity is TargetableObject && entity.Id >= Id)
            {
                // 碰撞事件将由 id 小的一方进行处理，避免重复处理。
                return;
            }

            ////TODO: 执行碰撞
            AIUtility.PerformCollision(this, entity);
        }

        protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(elapseSeconds, realElapseSeconds);

            //TODO: 游戏结束判定

        }
    }
}